package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.domain.HotelProducts;
import cn.wolfcode.trip.base.mapper.HotelMapper;
import cn.wolfcode.trip.base.mapper.HotelProductsMapper;
import cn.wolfcode.trip.base.query.HotelQueryObjext;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelProductsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class HotelProductsServiceImpl implements IHotelProductsService {

        @Autowired
        private HotelProductsMapper mapper;

        @Autowired
        private HotelMapper hotelMapper;

    //增加酒店信息
    @Override
    public void save(HotelProducts hotelProducts) {
        //获取当前用户的信息 获取当前用户的积分
        Long id = hotelProducts.getUser().getId();
        List<HotelProducts> products = mapper.selectByPrimaryKey(id);
        Long intergral=null;
        for (HotelProducts product : products) {
            intergral = product.getIntergral();
        }
        //获取当前的商品的积分
        Long id1 = hotelProducts.getHotel().getId();
        Hotel hotel = hotelMapper.selectByPrimaryKey(id1);
        Long price = hotel.getIntegralPrice();
        //获取现在的积分
        long prices = intergral - price;
        //更改用户的积分
        mapper.updateIntergral(prices,id);

        hotelProducts.setIntergral(prices);
        mapper.insert(hotelProducts);
    }

    //高级查询商品信息
    @Override
    public PageInfo hotelProducts(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<HotelProducts> list=mapper.queryForList(qo);
        return new PageInfo(list);
    }

    //修改商品信息
    @Override
    public void update(HotelProducts hotelProducts) {
        mapper.updateByPrimaryKey(hotelProducts);
    }




    //获取信息
    public PageInfo qeury(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"hp.buyTime desc");
        List<HotelProducts> list=mapper.queryForList(qo);
        return new PageInfo(list);
    }


    //修改产品的状态
    @Override
    public void put(HotelQueryObjext qo) {
        //查询当前用户的积分
        Hotel hotel = hotelMapper.selectByPrimaryKey(qo.getProductsId());
        Long orders = hotel.getOrders();
        orders = orders + 1;
        Long id = qo.getProductsId();
        hotelMapper.updateorders(orders,id);

        //修改用户的操作时间
        Date date = new Date();
        qo.setBuyTime(date);
        mapper.put(qo);
    }


    //查询单个对象的 集合...
    @Override
    public List<HotelProducts> selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }


}
